import './index.scss'
import { Card, Form, Input, Button, notification } from 'antd'

import { userLogin } from '@/store/modules/user'
import { useDispatch } from 'react-redux'
import logo from '@/assets/logo.png'
import { useNavigate } from 'react-router-dom'

const Login = () => {
  const navigate = useNavigate()
  const dispatch = useDispatch()

  // 提交表单
  const submit = async (data?: LoginForm) => {
    console.log('Success:', data)

    // 等请求完成后再进行后续的操作
    await dispatch(userLogin(data!))

    // 登录成功后跳转到首页
    navigate('/')

    notification.open({
      type: 'success',
      message: '成功',
      description: '🎉登录成功'
    })
  }

  return (
    <div className="login">
      <Card className="login-container">
        <img className="login-logo" src={logo} alt="" />
        {/* 登录表单 */}
        <Form validateTrigger={['onBlur']} onFinish={submit}>
          <Form.Item name="mobile"
            rules={[{ required: true, message: '请输入手机号' }, { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不对' }]}>
            <Input size="large" placeholder="请输入手机号" />
          </Form.Item>

          <Form.Item name="code"
            rules={[{ required: true, message: '请输入验证码' }, { len: 6, message: '验证码是个6位数的纯数字' }]}>
            <Input size="large" placeholder="请输入验证码" maxLength={6} />
          </Form.Item>

          <Form.Item>
            <Button type="primary" htmlType="submit" size="large" block>登录</Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}

export default Login